//
//  FSCalendarYearMonthView.m
//  THCalendar
//
//  Created by 魏鑫 on 2020/12/30.
//

#import "FSCalendarYearMonthView.h"
#import "FSCalendar.h"
#import "FSCalendarDynamicHeader.h"
#import "FSCalendarExtensions.h"

@interface FSCalendarYearMonthView ()

@property (weak  , nonatomic) FSCalendar *calendar;

- (void)commonInit;

@end

@implementation FSCalendarYearMonthView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    UILabel *yearMonthLabel = [UILabel new];
    [self addSubview:yearMonthLabel];
    self.yearMonthLabel = yearMonthLabel;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat labelLeft = 16;
    CGSize labelSize = [self.yearMonthLabel.text sizeWithAttributes:@{NSFontAttributeName:self.yearMonthLabel.font}];
    CGFloat labelTop = (self.frame.size.height - labelSize.height)/2.;
    self.yearMonthLabel.frame = CGRectMake(labelLeft, labelTop, self.bounds.size.width - labelLeft*2, labelSize.height);
}

- (void)setCalendar:(FSCalendar *)calendar
{
    _calendar = calendar;
    [self configureAppearance];
}

- (void)configureAppearance
{
    self.yearMonthLabel.font = self.calendar.appearance.yearmonthFont;
    self.yearMonthLabel.textColor = self.calendar.appearance.yearmonthTextColor;
    self.yearMonthLabel.textAlignment = self.calendar.appearance.yearmonthTitleAlignment;
}

- (void)setCurrentDate:(NSDate *)currentDate
{
    _currentDate = currentDate;
    self.yearMonthLabel.text = [self.calendar.formatter stringFromDate:currentDate];
    [self.yearMonthLabel sizeToFit];
}

@end
